﻿#include"FITKAcousticsDiscModelAdaptor.h"
#include "FITKAcousticsFDiscNodeListAdaptor.h"
#include "FITKAcousticsSEAModelAdaptor.h"
#include "FITKAcousticsFEModelAdaptor.h"
#include "FITKAcousticsSEAJunctionMgrAdaptor.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscNodeList.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEAAbstractJunction.h"

bool IO::FITKAcousticsDiscModelAdaptor::adaptR()
{
    Acoustics::FITKAcousticsDiscModel* discModel = dynamic_cast<Acoustics::FITKAcousticsDiscModel*>(_dataObj);
    Acoustics::FITKAcousticsDiscNodeList* Nodes = discModel->getNodes();//获取节点列表
    Acoustics::FITKAcousticsSEAModel*SEAModel = discModel->getSEAModel();//SEA模型
    Acoustics::FITKAcousticsFEModel* FEp = discModel->getFEModel();//获取FE模型
    Acoustics::FITKJunctionManager*JunctionManagerP = discModel->getJunctionManager();//交界处耦合管理器
    bool NodesRet = false;
    bool SEAModelRet = false;
    bool JunctionRet = false;
    bool FERet = false;
    if (Nodes != nullptr)
    {
        FITKAcousticsFDiscNodeListAdaptor* adaptor =
            FITKIOADAPTORFACTORY->createT<FITKAcousticsFDiscNodeListAdaptor>("FITKAcousticsHDF5", Nodes);
        if (adaptor)
        {
            adaptor->setReader(_reader);
            adaptor->setDataObject(Nodes);
            NodesRet = adaptor->adaptR();
            delete adaptor;
        }
    }

    if (SEAModel != nullptr)
    {
        FITKAcousticsSEAModelAdaptor* adaptor =
            FITKIOADAPTORFACTORY->createT<FITKAcousticsSEAModelAdaptor>("FITKAcousticsHDF5", SEAModel);
        if (adaptor)
        {
            adaptor->setReader(_reader);
            adaptor->setDataObject(SEAModel);
            SEAModelRet = adaptor->adaptR();
            delete adaptor;
        }
    }

    if (FEp != nullptr)
    {
        //FE模型数据适配器
        FITKAcousticsFEModelAdaptor* adaptor =
            FITKIOADAPTORFACTORY->createT<FITKAcousticsFEModelAdaptor>("FITKAcousticsHDF5", FEp);
        if (adaptor)
        {
            adaptor->setReader(_reader);
            adaptor->setDataObject(FEp);
            FERet = adaptor->adaptR();
            delete adaptor;
        }
    }


    if (JunctionManagerP != nullptr)
    {
        //Junction管理器适配器
        FITKAcousticsSEAJunctionMgrAdaptor* adaptor =
            FITKIOADAPTORFACTORY->createT<FITKAcousticsSEAJunctionMgrAdaptor>("FITKAcousticsHDF5", "Acoustics::FITKJunctionManager");
        if (adaptor == nullptr) return false;
        adaptor->setReader(_reader);
        adaptor->setData(JunctionManagerP);
        JunctionRet = adaptor->adaptR();
        delete adaptor;
    }
    return NodesRet && SEAModelRet &&JunctionRet&&FERet;
}

bool IO::FITKAcousticsDiscModelAdaptor::adaptW()
{
    Acoustics::FITKAcousticsDiscModel* discModel = dynamic_cast<Acoustics::FITKAcousticsDiscModel*>(_dataObj);

    Acoustics::FITKAcousticsDiscNodeList* Nodes = discModel->getNodes();//获取节点列表
    Acoustics::FITKAcousticsSEAModel* SEAp = discModel->getSEAModel();//获取SEA模型
    Acoustics::FITKAcousticsFEModel* FEp = discModel->getFEModel();//获取FE模型
    Acoustics::FITKJunctionManager*JunctionManagerP =discModel->getJunctionManager();//交界处耦合管理器

    bool NodesRet = false;
    bool SEARet = false;
    bool FERet = false;
    bool JunctionManagerRet = false;

    //回写顺序敏感
    if (Nodes != nullptr)
    {

        //节点列表数据适配器
        FITKAcousticsFDiscNodeListAdaptor* adaptor =
            FITKIOADAPTORFACTORY->createT<FITKAcousticsFDiscNodeListAdaptor>("FITKAcousticsHDF5", Nodes);
        if (adaptor)
        {
            adaptor->setWriter(_writer);
            adaptor->setDataObject(Nodes);
            NodesRet = adaptor->adaptW();
            delete adaptor;
        }
    }

    if (SEAp != nullptr)
    {
        //SEA模型数据适配器
        FITKAcousticsSEAModelAdaptor* adaptor =
            FITKIOADAPTORFACTORY->createT<FITKAcousticsSEAModelAdaptor>("FITKAcousticsHDF5", SEAp);
        if (adaptor)
        {
            adaptor->setWriter(_writer);
            adaptor->setDataObject(SEAp);
            SEARet = adaptor->adaptW();
            delete adaptor;
        }
    }
    if (JunctionManagerP != nullptr)
    {
        //Junction管理器适配器
        FITKAcousticsSEAJunctionMgrAdaptor* adaptor =
            FITKIOADAPTORFACTORY->createT<FITKAcousticsSEAJunctionMgrAdaptor>("FITKAcousticsHDF5", "Acoustics::FITKJunctionManager");
        if (adaptor == nullptr) return false;
        adaptor->setWriter(_writer);
        //adaptor->setDataObject(JunctionManagerP);
        adaptor->setData(JunctionManagerP);
        JunctionManagerRet = adaptor->adaptW();
        delete adaptor;
    }

    if (FEp != nullptr)
    {
        //FE模型数据适配器
        FITKAcousticsFEModelAdaptor* adaptor =
            FITKIOADAPTORFACTORY->createT<FITKAcousticsFEModelAdaptor>("FITKAcousticsHDF5", FEp);
        if (adaptor)
        {
            adaptor->setWriter(_writer);
            adaptor->setDataObject(FEp);
            FERet = adaptor->adaptW();
            delete adaptor;
        }
    }

    return NodesRet && SEARet&& FERet;
}
